package org.hackystat.sdsa.analyzer.model.activity.edit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import jess.Fact;

import org.hackystat.sdsa.analyzer.model.activity.ActivityTestBase;
import org.hackystat.sdsa.analyzer.model.activity.TestCompilationActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;

/**
 * Tests documentation edit activity. 
 * 
 * @author Hongbing Kou
 */
public class TestDocumentEditActivity extends ActivityTestBase {
  /** Document edit sensor data. */
  private SensorData sensorData;
  private Logger logger;
  
  /**
   * Sets up test environment.
   * 
   * @throws Exception If any error.
   */
  @Override public void setUp() throws Exception {
    super.setUp();  
    this.logger = Logger.getLogger(TestCompilationActivity.class.getName());
    
    this.sensorData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2005-01-01T08:30:45.000-10:00");
    sensorData.setTimestamp(tstamp);
    sensorData.setResource("file:/C:/cvs/work/com/foo/package.html");
    sensorData.setSensorDataType("DevEvent");

    Properties props = new Properties();
    sensorData.setProperties(props);
    
    Property type = new Property();
    type.setKey("Type");
    type.setValue("Edit");
    props.getProperty().add(type);
    
    Property subType = new Property();
    subType.setKey("Subtype");
    subType.setValue("StateChange");
    props.getProperty().add(subType);
    
    Property currentSize = new Property();
    currentSize.setKey(EditActivity.CURRENT_SIZE);
    currentSize.setValue("269");
    props.getProperty().add(currentSize);
  }
  
  /**
   * Tests documentation edit.
   * 
   * @throws Exception If error in testing. 
   */
  @Test public void testDocumentEdit() throws Exception {
    DocumentEditActivity activity = new DocumentEditActivity(sensorData, this.logger);
    
    assertEquals("Test duration", 0, activity.getDuration());
    assertEquals("Test file size", 269, activity.getFileSize());
    assertEquals("Test file size increase", 0, activity.getFileSizeIncrease());
    assertEquals("Test action string", "2005-01-01T08:30:45.000-10:00 package.html " + 
                                       "DOCUMENT EDIT 0s {FI=0(269)}",
                                        activity.toString());
  }
  
  /**
   * Tests conversion from action object to JESS fact.
   * 
   * @throws Exception If error while converting to Jess fact.
   */
  @Override public void testJessFactConversion() throws Exception {
    DocumentEditActivity action = new DocumentEditActivity(this.sensorData, this.logger);
    action.setDuration(200);
    action.setFileSizeIncrease(15);
    Fact assertedFact = action.assertActivity(1, engine);
    assertTrue("Test action is asserted to working memory", assertedFact.getFactId() >= 0);
  }
  
  /**
   * Test bounday condiction for resource.
   * 
   */
  @Test public void testBoundary() {
    this.sensorData.setResource("http://www.hawaii.edu/~foo/Bar.java");
    DocumentEditActivity activity = new DocumentEditActivity(this.sensorData, this.logger);
    assertEquals("Resouce file is empty if it is not a file resource", "", 
        activity.getResourceFile());
    
    this.sensorData.setResource("C:/Bar.java");
    activity = new DocumentEditActivity(this.sensorData, this.logger);
    assertEquals("Resouce file is empty if it is not a valid resource", "", 
        activity.getResourceFile());
  }
}
